// i.d(t, {
//   S7: () => l,
//   Pm: () => d,
//   lT: () => m,
//   q6: () => g,
//   t3: () => f,
//   Oc: () => v,
//   Bl: () => y,
//   RQ: () => b
// })

import * as THREE from "three"
import funcUtil from "./func.util"
import lodEnum from "../enum/lod.enum"
import chunkEnum, { chunkType } from "../enum/chunk.enum"
const chunkTileSize = Number(funcUtil.getUrlParams("chunkTileSize", 512))
const damConfig = {
    [chunkEnum.LOW]: {
      debugColor: new THREE.Vector4(1, 0, 0, 0.5),
      textureSize: 256,
      tileSize: 256,
      assetType: "low",
      assetSize: 512,
      texelSize: 0.096
    },
    [chunkEnum.MEDIUM]: {
      debugColor: new THREE.Vector4(1, 1, 0, 0.5),
      textureSize: 512,
      tileSize: 512,
      assetType: "low",
      assetSize: 512,
      texelSize: 0.048
    },
    [chunkEnum.HIGH]: {
      debugColor: new THREE.Vector4(0, 1, 0, 0.5),
      textureSize: 1024,
      tileSize: 512,
      assetType: "high",
      assetSize: 2048,
      texelSize: 0.024
    },
    [chunkEnum.ULTRA]: {
      debugColor: new THREE.Vector4(0, 1, 1, 0.5),
      textureSize: 2048,
      tileSize: 512,
      assetType: "high",
      assetSize: 2048,
      texelSize: 0.012
    }
  },
  damIds = [chunkEnum.LOW, chunkEnum.MEDIUM, chunkEnum.HIGH, chunkEnum.ULTRA],
  getDamIndex = (e: number) => {
    for (const t in damConfig) if (damConfig[t].textureSize === e) return parseInt(t, 10)
    throw new Error(`Not a valid texture size: ${e}`)
  },
  tileConfig = {
    [chunkEnum.EMBEDDED_0_64]: {
      debugColor: new THREE.Vector4(1, 0, 0, 0.1),
      textureSize: 2048,
      tileSize: 2048,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.064
    },
    [chunkEnum.EMBEDDED_1_32]: {
      debugColor: new THREE.Vector4(1, 0.2, 0.2, 0.3),
      textureSize: 1024,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.032
    },
    [chunkEnum.STREAM_1_16]: {
      debugColor: new THREE.Vector4(0, 1, 0, 0.3),
      textureSize: 2048,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.016
    },
    [chunkEnum.EMBEDDED_2_16]: {
      debugColor: new THREE.Vector4(1, 0.3, 1, 0.5),
      textureSize: 1024,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.016
    },
    [chunkEnum.STREAM_2_8]: {
      debugColor: new THREE.Vector4(0.2, 0.3, 1, 0.5),
      textureSize: 2048,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.008
    },
    [chunkEnum.STREAM_2_4]: {
      debugColor: new THREE.Vector4(0.2, 1, 0.2, 0.5),
      textureSize: 4096,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.004
    },
    [chunkEnum.EMBEDDED_3_4]: {
      debugColor: new THREE.Vector4(1, 0.7, 0, 0.7),
      textureSize: 1024,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.004
    },
    [chunkEnum.STREAM_3_2]: {
      debugColor: new THREE.Vector4(0, 0.7, 0, 0.7),
      textureSize: 2048,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.002
    },
    [chunkEnum.STREAM_3_1]: {
      debugColor: new THREE.Vector4(0, 1, 0, 0.7),
      textureSize: 4096,
      tileSize: chunkTileSize,
      assetType: "max",
      assetSize: 4096,
      texelSize: 0.001
    }
  },
  lodToTile = {
    [lodEnum.Min]: [chunkEnum.EMBEDDED_0_64],
    [lodEnum.Standard]: [chunkEnum.EMBEDDED_1_32, chunkEnum.STREAM_1_16],
    [lodEnum.High]: [chunkEnum.EMBEDDED_2_16, chunkEnum.STREAM_2_8, chunkEnum.STREAM_2_4],
    [lodEnum.Detail]: [chunkEnum.EMBEDDED_3_4, chunkEnum.STREAM_3_2, chunkEnum.STREAM_3_1]
  },
  getConfig = (e = chunkType.DAM) => (e === chunkType.TILE ? tileConfig : damConfig),
  getMinQuality = (e = chunkType.DAM, t = lodEnum.Min) => (e === chunkType.TILE ? lodToTile[t][0] : damIds[0]),
  getMaxQuality = (e = chunkType.DAM, t = lodEnum.Detail) => (e === chunkType.TILE ? lodToTile[t][lodToTile[t].length - 1] : damIds[damIds.length - 1]),
  getOrder = (e = chunkType.DAM, t = 0) => (e === chunkType.TILE ? lodToTile[t] : damIds),
  canUseID = (e, t, i) => {
    const n = getOrder(t, i)
    let s = n[0]
    const r = n[n.length - 1],
      o = n.indexOf(e)
    return o + 1 === n.length ? r : (-1 !== o && (s = n[o + (1 % n.length)]), s)
  }
function canUseQuality(e, t, i) {
  const n = getOrder(t, i),
    s = getConfig(t),
    r = getMinQuality(t, i)
  let o = getMaxQuality(t, i)
  for (let t = n.length - 1; t >= 0; t--) e > s[n[t]].texelSize && o > r && (o = n[t])
  return o
}
export default {
  getDamIndex,
  getConfig,
  getMinQuality,
  getMaxQuality,
  getOrder,
  canUseID,
  canUseQuality
}
